package com.yy.micro.magic.data.model.converter;

import com.yy.micro.magic.data.model.utils.JsonUtils;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.AttributeConverter;

public class TypedMapConverter implements AttributeConverter<Map,String> {
    @Override
    public String convertToDatabaseColumn(Map map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, Object> originMap = map;
        return JsonUtils.toJson(map);
    }

    @Override
    public Map convertToEntityAttribute(String dbData) {
        if (dbData == null || dbData.trim().length() == 0) {
            return new HashMap();
        }
        return JsonUtils.fromJson(dbData,Map.class);
    }
}
